package com.umeng.soexample.mvp.persenter;

import com.umeng.lib_core.mvp.presenter.BasePresenter;
import com.umeng.soexample.bean.BaseEntity;
import com.umeng.soexample.bean.HomeBannerEntity;
import com.umeng.soexample.bean.HomeGoodsEntity;
import com.umeng.soexample.bean.HomeRecommendEntity;
import com.umeng.soexample.mvp.contract.HomeFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomeFragmentPresenter extends BasePresenter<HomeFragmentContract.HomeFragmentIView, HomeFragmentContract.HomeFragmentIModel> {

    private int page = 1;
    private String category_id = "1";
    @Inject
    public HomeFragmentPresenter(HomeFragmentContract.HomeFragmentIView homeFragmentIView, HomeFragmentContract.HomeFragmentIModel homeFragmentIModel) {
        super(homeFragmentIView, homeFragmentIModel);
    }

    //发起全部网络请求
    public void requestAll(){
        page = 1;
        Map<String, String> map = new HashMap<>();
        JSONObject recommend = new JSONObject();
        try {
            recommend.put("menu", 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("recommend", recommend.toString());
        JSONObject goods = new JSONObject();
        try {
            goods.put("category", category_id);
            goods.put("page", "" + page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("goods", goods.toString());
        m.request(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(BaseEntity baseEntity) {
                        //区分返回值
                        if (baseEntity instanceof HomeBannerEntity){
                            v.bannerSuccess((HomeBannerEntity) baseEntity);
                        }else if (baseEntity instanceof HomeRecommendEntity){
                            v.recommendSuccess((HomeRecommendEntity) baseEntity);
                        }else if (baseEntity instanceof HomeGoodsEntity){
                            v.goodsSuccess((HomeGoodsEntity) baseEntity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void refresh(String category_id) {
        page = 1;
        this.category_id = category_id;
        JSONObject goods = new JSONObject();
        try {
            goods.put("category", category_id);
            goods.put("page", "" + page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.requestGoods(goods.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomeGoodsEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                    }

                    @Override
                    public void onNext(HomeGoodsEntity homeGoodsEntity) {
                        v.hideDialog();
                        v.goodsSuccess(homeGoodsEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    public void mine_request(){

        JSONObject goods = new JSONObject();
        try {
            goods.put("category", "1");
            goods.put("page", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.requestGoods(goods.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomeGoodsEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                    }

                    @Override
                    public void onNext(HomeGoodsEntity homeGoodsEntity) {
                        v.hideDialog();
                        v.goodsSuccess(homeGoodsEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });

    }

    public void loadMore() {
        page++;
        JSONObject goods = new JSONObject();
        try {
            goods.put("category", category_id);
            goods.put("page", "" + page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.requestGoods(goods.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomeGoodsEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(HomeGoodsEntity homeGoodsEntity) {
                        v.goodsSuccess(homeGoodsEntity);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
